﻿using HighPerformanceLog.V2;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LogViewerUI
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<HWLog> logList = new List<HWLog>();

        /// <summary>
        /// 返回指定控件中的所有控件。
        /// </summary>
        /// <param name="control">待遍历控件。</param>
        /// <param name="controls">控件列表。</param>
        /// <returns></returns>
        public List<UIElement> GetAllControls(object control)
        {
            List<UIElement> controls = new List<UIElement>();
            // 带有Children的窗口，包括Grid、StackPanel、DockPanel等常用容器。
            if (control is Panel panel && panel.Children != null)
            {
                foreach (var subControl in panel.Children)
                    controls.AddRange(GetAllControls(subControl));
            }
            // 象Border之类的都属于此类。
            else if (control is Decorator border)
            {
                controls.AddRange(GetAllControls(border.Child));
            }
            // 需要接收的几类控件
            else if (control is TextBox || control is ComboBox || control is CheckBox)
            {
                controls.Add(control as UIElement);
            }

            return controls;
        }
        public MainWindow()
        {
            InitializeComponent();

            // 参考资料： https://www.zhihu.com/tardis/bd/art/426818257?source_id=1001
            colTime.HeaderStringFormat = "yyyy/MM/dd HH:mm:ss.fff";
            logList = HWLog.LoadLogs("Samples\\2023-09-26.log");
            lvUsers.ItemsSource = logList;

            HWLog log = new HWLog();
            log.AttachStackFramInfo();
            // HighPerformanceLog.V1.Functions.AttachStackFrameToLog(log);
            System.Diagnostics.Debug.WriteLine(log);

            foreach (var item in stkSeverity.Children)
            {
                if (item is CheckBox ck)
                {
                    ck.Checked += (s, e) => UpdateUIData();
                    ck.Unchecked += (s, e) => UpdateUIData();
                }
            }

            colDate.DisplayMemberBinding.StringFormat = "yyyy-MM-dd";
            UpdateUIData();
        }

        private void UpdateUIData()
        {
            List<HWLog> list = new List<HWLog>();

            TimeSpan startTime = TimeSpan.Parse(tbStartTime.Text);
            TimeSpan endTime = TimeSpan.Parse(tbEndTime.Text);
            foreach (var log in logList)
            {
                if (log.Time.TimeOfDay < startTime || log.Time.TimeOfDay > endTime)
                    continue;

                if (ckFatal.IsChecked == true && log.Severity == LogSeverity.FATAL)
                    list.Add(log);

                if (ckError.IsChecked == true && log.Severity == LogSeverity.ERROR)
                    list.Add(log);

                if (ckWarning.IsChecked == true && log.Severity == LogSeverity.WARNING)
                    list.Add(log);


                if (ckInfo.IsChecked == true && log.Severity == LogSeverity.INFO)
                    list.Add(log);


                if (ckDebug.IsChecked == true && log.Severity == LogSeverity.DEBUG)
                    list.Add(log);
            }

            this.Resources["s1"] = "hello";
            var v2 = Resources["s2"];
            Debug.WriteLine(v2 == null);

            lvUsers.ItemsSource = list;
            sbListItemCount.Content = $"日志数量：{list.Count}";
        }

        private void btnFilter_Click(object sender, RoutedEventArgs e)
        {
            UpdateUIData();
        }

        private void tbStartTime_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!DateTime.TryParse(tbStartTime.Text, out DateTime res))
                tbStartTime.Text = "00:00:00";
        }

        private void tbEndTime_LostFocus(object sender, RoutedEventArgs e)
        {
            if (!DateTime.TryParse(tbEndTime.Text, out DateTime res))
                tbEndTime.Text = "23:59:59";
        }
    }
}
